package com.wontube.smr.remote.web;

import java.util.List;

import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.cache.Cacher;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.dao.bo.ViewHistoryDao;
import com.wontube.smr.web.client.IViewHistoryHandler;
import com.wontube.smr.web.client.model.po.ViewHistory;

/**
 * @author tanggf(2012-5-30)
 *
 */
@NoLoginSupport
public class ViewHistoryHandler extends BasicHandler implements IViewHistoryHandler {

	@Override
	@CacheSupport
	public List<ViewHistory> listLatestViewHistories(String session, int limit) {
		return ViewHistoryDao.listLatestViewHistories(session, limit);
	}

	@Override
	public Integer deleteAllViewHistories(String session) {
		int aff = ViewHistoryDao.deleteAllViewHistory(session);
		if(aff > 0){
			cleanViewHistoryCaches();			
		}
		return aff;
	}

	@Override
	public Integer deleteViewHistory(String session, String id) {
		int aff = ViewHistoryDao.deleteViewHistory(session, id);
		if(aff == 1){
			cleanViewHistoryCaches();			
		}
		return aff;
	}

	@Override
	public Integer addOrUpdateViewHistory(ViewHistory history) {
		int aff = ViewHistoryDao.addOrUpdateViewHistory(history);
		if(aff == 1){
			cleanViewHistoryCaches();			
		}
		return aff;
	}
	
	private void cleanViewHistoryCaches(){
		String clazzName = IViewHistoryHandler.class.getName();
		Cacher.defaultCacher.deleteAllCache(clazzName, "listLatestViewHistories", 2);
	}
}
